package com.assembly.system.controller;


import com.assembly.entities.Role;
import com.assembly.system.req.RoleREQ;
import com.assembly.system.service.IRoleResourcesService;
import com.assembly.system.service.IRoleService;
import com.assembly.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 平台角色信息表 前端控制器
 * </p>
 *
 * @author 丽人平台-www.assembly.com
 * @since 2021-03-29
 */
@Api(value = "角色管理", description = "提供角色的增、删、改、查、授权")
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 注入 角色service
     */
    @Autowired
    private IRoleService roleService;
    /**
     * 注入 角色资源关联Service
     */
    @Autowired
    private IRoleResourcesService roleResourcesService;
    /****
     * 新增角色
     * @param role
     * @return
     */
    @ApiOperation("新增角色")
    @PostMapping
    public Result saveRole(@RequestBody  Role role){
        roleService.saveRole(role);
        return  Result.ok();
    }

    /**
     * 根据角色ID删除 角色
     * @param roleIds 角色ID集合
     * @return
     */
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "roleIds", value = "角色ID集合", required = true)
    @DeleteMapping
    public Result deleteRole(List<String> roleIds){
        roleService.deleteRole(roleIds);
        return  Result.ok();
    }

    /**
     * 修改 角色信息
     * @param role 角色对象
     */
    @ApiOperation("修改角色")
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return Result.ok();
    }

    /***
     * 根据角色ID查询ID详情
     * @param roleId 角色ID
     * @return
     */
    @ApiOperation("根据角色ID查询ID详情")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    @GetMapping({"{roleId}"})
    public Result queryRoleById(@PathVariable("roleId") String roleId){

       return  Result.ok( roleService.getById(roleId));
    }

    /***
     *根据 机构（店铺 or 学校机构ID ） 查询 角色下拉列表
     * @param mechanismId 机构（店铺 or 学校机构ID ）
     * @return
     */
    @ApiOperation("根据角色ID查询ID详情")
    @ApiImplicitParam(name = "mechanismId", value = "机构Id", required = true)
    @GetMapping("drop/{mechanismId}")
    public  Result queryDropDownByMechanismId(@PathVariable("mechanismId") String mechanismId){

        return roleService.queryDropDownByMechanismId(mechanismId);
    }

    /***
     * 角色列表查询
     * @param roleREQ
     * @return
     */
    @ApiOperation("根据条件查询角色列表")
    @ApiImplicitParam(name = "mechanismId", value = "机构Id", required = true)
    @PostMapping ("search")
    public  Result  search(@RequestBody RoleREQ roleREQ){

            return Result.ok(roleService.search(roleREQ));
    }


    /***
     * 根据角色ID查询 该角色下有那些 资源
     * @param roleId
     * @return
     */
    @ApiOperation("根据角色ID查询资源信息")
    @ApiImplicitParam(name = "mechanismId", value = "机构Id", required = true)
    @GetMapping ("role/resources/{roleId}")
    public  Result  queryResourcesIdByRoleId(@PathVariable("roleId") String roleId){

        List<String> resourcesIds = roleResourcesService.queryResourcesIdByRoleId(roleId);

        return  Result.ok(resourcesIds);
    }

    /***
     * 新增 或修改 角色权限
     * @param roleId
     * @param resourcesIds
     * @return
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(name = "resourcesIds", value = "资源ID集合", required = true,
                    allowMultiple = true, dataType = "String"),
    })
    @ApiOperation("新增角色与资源关系接口")
    @PostMapping("save/{roleId}")
    public Result  saveOrUpdateResourcesAndRole(@PathVariable("roleId")String roleId,
                                                @RequestBody  List<String> resourcesIds){
        return  Result.ok(roleResourcesService.saveOrUpdateResourcesAndRole(roleId,resourcesIds));
    }


}
